package com.daiji.dao.impl;

import com.daiji.dao.StudentDao;
import com.daiji.entity.StudentEntity;
import com.daiji.util.JDBCUtil;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class StudentDaoImpl implements StudentDao {

    /**
     * 查询所有的学生信息
     * @return
     */
    @Override
    public List<StudentEntity> selectStudents(StudentEntity student) {
//        String sql = "select * from student";
        StringBuilder sql = new StringBuilder("select * from student ");
        if (student.getName()!=null && !"".equals(student.getName())){
            sql.append("where name like '%"+student.getName()+"%' ");
        }
        sql.append("limit ?,?");
        List<Map<String, Object>> list = JDBCUtil.queryMap(sql.toString(),(student.getPage()-1)*student.getLimit(),student.getLimit());

        List<StudentEntity> list1 = new ArrayList<>();
        if (list.size()>0){
            for (Map map : list) {
                StudentEntity student1 = new StudentEntity();
                try {
                    BeanUtils.populate(student1, map);
                    list1.add(student1);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
            return list1;
        }

        return null;
    }

    /**
     * 获取到表中有多少行记录
     * @param student
     * @return
     */
    @Override
    public int getCount(StudentEntity student) {
        StringBuilder sql = new StringBuilder("select count(*) from student ");
        if (student.getName()!=null && !"".equals(student.getName())){
            sql.append("where name like '%"+student.getName()+"%' ");
        }

        List<List> list = JDBCUtil.queryList(sql.toString());
        if (list.size()>0){
            Object o = list.get(0).get(0);

            return Integer.parseInt(o.toString());
        }
        return -1;
    }

    /**
     * 新增一条学生信息
     * @param student
     * @return
     */
    @Override
    public int insertStudent(StudentEntity student) {
        String sql = "insert into student values(null,?,?,?,?)";
        int i = JDBCUtil.insert(sql, student.getName(), student.getSex(), student.getBirthday(), student.getTel());

        return i;
    }

    /**
     * 删除一条学生信息
     * @param id
     * @return
     */
    @Override
    public int deleteStudent(Integer id) {
        String sql = "delete from student where id=?";
        int i = JDBCUtil.update(sql, id);

        if (i>0)
            return i;

        return -1;
    }

    /**
     * 修改学生的信息
     * @param student
     * @return
     */
    @Override
    public int updateStudent(StudentEntity student) {
        String sql = "update student set name=?,sex=?,birthday=?,tel=? where id=?";
        int i = JDBCUtil.update(sql, student.getName(), student.getSex(), student.getBirthday(), student.getTel(), student.getId());

        return i;
    }
}
